<template>
  <div class="user-edit">
    <!-- Drawer抽屉 -->
    <Drawer :title="title" v-model="visible" width="720" draggable :mask-closable="type=='0'">
      <div :style="{maxHeight: maxHeight}" class="drawer-content">
        <Form ref="form" :model="form" :rules="formValidate" label-position="top">
          <FormItem label="隐患内容" prop="hiddenDangerContent">
            <Input v-model="form.hiddenDangerContent" type="textarea" />
          </FormItem>
          <FormItem label="隐患依据" prop="hiddenDangerAccording">
            <Input v-model="form.hiddenDangerAccording" type="textarea" />
          </FormItem>
          <Row :gutter="32">
            <Col span="24">
              <FormItem label="隐患图片" prop="hiddenDangerPics">
                <upload-pic-thumb
                    @on-change="form.hiddenDangerPics=$event" 
                    style="width:100%" 
                    ref="uploadPics"
                ></upload-pic-thumb>
              </FormItem>
            </Col> 
          </Row>
        </Form>
      </div>
      <div class="drawer-footer br" v-show="type!=='0'">
        <Button type="primary" :loading="submitLoading" @click="submit">提交</Button>
        <Button @click="visible = false">取消</Button>
      </div>
    </Drawer>
  </div>
</template>

<script>
import { 
  createOrUpdateHiddenDangerRecord, 
  getHiddenDangerRecordDetail
} from "@/api/index";
import uploadPicThumb from "@/views/my-components/xboot/upload-pic-thumb";
export default {
  name: "break",
  components: {
    uploadPicThumb
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object
    },
    type: {
      type: String,
      default: "0"
    }
  },
  data() {
    return {
      jobData: [],
      visible: this.value,
      title: "",
      submitLoading: false,
      maxHeight: 510,
      form: {},
      formValidate: {
        // 表单验证规则
        hiddenDangerContent: [{ required: true, message: "请输入隐患内容", trigger: "blur" }],
        hiddenDangerAccording: [
          { required: true, message: "请输入隐患依据", trigger: "blur" }
        ]
      },
      jobName: []
    };
  },
  methods: {
    init() {
      
    },
    submit() {
      console.log(this.form)
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.type === "1") {
            // 编辑
            let temp = this.$refs.uploadPics.uploadList;
            let array = []
            temp.forEach(element => {
              array.push(element.url)
            })
            this.form.hiddenDangerPics = array.join(',');
            this.submitLoading = true;
            createOrUpdateHiddenDangerRecord(this.form).then(res => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.$emit("on-submit", true);
                this.visible = false;
              }
            });
          } else {
            // 添加
            console.log('add')
            let temp = this.$refs.uploadPics.uploadList;
            let array = []
            temp.forEach(element => {
              array.push(element.url)
            })
            this.form.hiddenDangerPics = array.join(',');
            this.submitLoading = true;
            createOrUpdateHiddenDangerRecord(this.form).then(res => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.$emit("on-submit", true);
                this.visible = false;
              }
            })
          }
        }
      });
    },
    setCurrentValue(value) {
      console.log(value)
      
      if (value === this.visible) {
        return;
      }
      if (this.type === "1") {
        this.title = "编辑隐患记录";
        this.maxHeight = "100%";
        // this.maxHeight = Number(document.documentElement.clientHeight - 121) + "px";
      } else if (this.type === "2") {
        this.title = "添加隐患记录";
        this.maxHeight = "100%";
      } else {
        this.title = "隐患记录详情";
        this.maxHeight = "100%";
      }
      console.log(this.data)
      // 清空数据
      this.$refs.form.resetFields();
      if (this.type === "0" || this.type === "1") {
        getHiddenDangerRecordDetail({ id: this.data.id }).then(res => {
          if (res.success) {
            let data = res.result;
            this.form = this.data;
            this.form.hiddenDangerAccording = data.hiddenDangerAccording;
            this.$refs.uploadPics.uploadList = [];
            this.$refs.uploadPics.setData(data.hiddenDangerPicList) 
          }
        }) 
      } else {
        this.form = {
          enterpriseUserId: this.data.enterpriseUserId,
          hiddenDangerContent: "",
          hiddenDangerAccording: "",
          hiddenDangerPics: null
        };
      }
      this.visible = value;
    }
  },
  watch: {
    value(val) {
      this.setCurrentValue(val);
    },
    visible(value) {
      this.$emit("input", value);
    }
  },
  mounted() {
  }
};
</script>

<style lang="less">
@import "../../../styles/table-common.less";
.drawer-content {
  overflow: auto;
}
.drawer-content::-webkit-scrollbar {
  display: none;
}
.user-title {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  .info-title {
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
    display: block;
    margin-right: 16px;
  }
}
.info-header {
  font-size: 16px;
  color: rgba(0, 0, 0, 0.85);
  display: block;
  margin-bottom: 16px;
}
</style>

